package com.xtr.service;

import com.Client1Application;
import com.lcn.comm.util.DateUtil;
import com.weibo.api.motan.util.LoggerUtil;
import com.xtr.tx.service.SysTestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @createTime: 2017/1/12 14:17
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Client1Application.class)// 指定spring-boot的启动类
public class SpringBootTest1 {


    private ExecutorService pool;

    @Resource
    private SysTestService sysTestService;


    @Test
    public void test7() throws Exception {
        LoggerUtil.info("测试·······················");
        Long startTime = System.currentTimeMillis();
        List<Future> rowResult = new CopyOnWriteArrayList<Future>();
        pool = Executors.newFixedThreadPool(30);
        for (int i = 0; i < 1000; i++) {
            final int finalI = i;
            rowResult.add(pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        sysTestService.test();
//                        sysTestService.test1();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }));
        }
        //等待处理结果
        for (Future f : rowResult) {
            f.get();
        }
        //启动一次顺序关闭，执行以前提交的任务，但不接受新任务。如果已经关闭，则调用没有其他作用
        pool.shutdown();
        System.out.println("测试耗时：" + DateUtil.dateDiff(startTime, System.currentTimeMillis()));
    }
}
